package com.findyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.findyou.pojo.User;
import com.findyou.service.UserService;
import com.findyou.mapper.UserMapper;
import com.findyou.utils.JwtHelper;
import com.findyou.utils.MD5Util;
import com.findyou.utils.Result;
import com.findyou.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-03-06 20:06:46
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 登录的业务
     * 对比密码
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);
        if(loginUser == null) {
            return  Result.build(null, 501, "用户名有误");
        }
        //登录成功
        if(!StringUtils.isEmpty(user.getUserPwd())
        && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
            Map<String, String >mp = new HashMap<>();
            mp.put("token", jwtHelper.createToken((long) loginUser.getUid()));
            return Result.ok(mp);
        }
       return Result.build(null, 503, "密码有误");
    }

    @Override
    public Result getUserInfo(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if(expiration) { // 失效
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Long id = jwtHelper.getUserId(token);
        User user = userMapper.getUserInfo(id);
        // 注意 密码 不要返回
        user.setUserPwd("");

        Map<String, User> loginUser = new HashMap<>();
        loginUser.put("loginUser", user);

        return Result.ok(loginUser);
    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(lambdaQueryWrapper); // 这里也可以用 selectCount()
        int rows = userMapper.checkUserName(username);
        //  System.out.println("rows = " + rows);
        //  System.out.println(username);
        if(rows == 0) { // 表示可用
            return Result.ok(null);
        } else {// 被占用了
            return Result.build(null, 505, "用户名占用");
        }
    }

    @Override
    public Result regist(User user) {
        int rows = userMapper.checkUserName(user.getUsername());
        if(rows == 0) { // 表示可用
            //注意 插入到数据库里面的密码是加密过的
            user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
            userMapper.regist(user);
            return Result.ok(null);
        } else {// 被占用了
            return Result.build(null, 505, "用户名占用");
        }
    }

    @Override
    public Result checkLogin(String token) { // 注意可能传过来的是空的 Wc 这一点没想到
        if(jwtHelper.isExpiration(token) || token == null) {
            return Result.build(null, 504, "loginExpired");
        } else {
            return Result.ok(null);
        }
    }

}




